﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class OrderDetailStatusController
    {
        public IList<OrderDetailStatus> GetAllOrderDetailStatus()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<OrderDetailStatus>().ToList();
            }
        }

        public IList<String> GetAllOrderDetailStatusStatus()
        {
            return GetAllOrderDetailStatus().Select(orderStatus => orderStatus.Status).ToList();
        }

        public int AddOrderDetailStatus(OrderDetailStatus orderDetailStatus)
        {
            try
            {
                GetAllOrderDetailStatus()
                    .Where(o => o.Status == orderDetailStatus.Status)
                    .Select(o => o)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(orderDetailStatus);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditOrderDetailStatus(OrderDetailStatus newOrderDetailStatus, String oldStatus)
        {
            try
            {
                var checkOrderDetailStatus = GetAllOrderDetailStatus()
                    .Where(o => o.Status == newOrderDetailStatus.Status)
                    .Select(o => o)
                    .Single();

                if (checkOrderDetailStatus.Status == oldStatus)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newOrderDetailStatus);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newOrderDetailStatus);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeleteOrderDetailStatus(OrderDetailStatus orderDetailStatus)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(orderDetailStatus);
                    transaction.Commit();
                }
            }
        }
    }
}
